<?php defined('SYSPATH') or die('No direct access allowed.');

	require_once("libraries/pChart2.1.1/class/pDraw.class.php");
	require_once("libraries/pChart2.1.1/class/pImage.class.php");
	require_once("libraries/pChart2.1.1/class/pData.class.php");

	class Model_Chart extends Model{
		
		private $pchart = NULL;

		public function create($data,$settings){
			
			ini_set("memory_limit","20M");
					
			$dataset = new pData();
			$dataset->addPoints($data,$settings->seriesName); //seriesName
			$dataset->setAxisName(0,$settings->YAxisLabel); //y-label
			$dataset->setAbscissaName($settings->XAxisLabel); //x-label
			$dataset->setPalette($settings->seriesName,$settings->seriesColor); //seriesColor en RGB
						
			$scaleSettings = array("Mode"=>SCALE_MODE_MANUAL,"ManualScale"=>$settings->axisBoundaries,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE,"LabelSkip"=>$settings->dataLabelSkip,"GridR"=>200,"GridG"=>200,"GridB"=>200);
			
			//Creo un chart image de 150px de ancho por 150px alto
			$this->pchart = new pImage($settings->chartWidth,$settings->chartHeight);
			//$this->pchart->Antialias = false;
			
			if (!$settings->blackWhite){
				$this->pchart->drawGradientArea(0,0,$settings->chartWidth,$settings->chartHeight,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>255,"EndG"=>255,"EndB"=>255,"Alpha"=>100));	
			}
			
			/* Add a border to the picture */
			$this->pchart->drawRectangle(0,0,$settings->chartWidth - 1,$settings->chartHeight - 1,array("R"=>0,"G"=>0,"B"=>0));
			
			$this->pchart->setDataSet($dataset); //asigno el dataset
			
			$this->pchart->drawFilledRectangle(30,30,$settings->chartWidth - 30,$settings->chartHeight - 30,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10));
	
			//El area de graficacion debe suministrarse y por lo general es menor que la imagen
			//para dejar lugar a las barras, los labels y los ejes
			//$chart->setGraphArea(20,20,270,70);
			$this->pchart->setGraphArea(30,30,$settings->chartWidth - 30,$settings->chartHeight - 30);
	
			/* Choose a nice font */
			$this->pchart->setFontProperties(array("FontName"=>"libraries/pChart2.1.1/fonts/verdana.ttf","FontSize"=>6));

			$this->pchart->drawText(30,25,$settings->chartTitle,array("FontSize"=>9,"Align"=>TEXT_ALIGN_BOTTOMLEFT)); //Segment title 
			
			
			//Importante metodo
			$this->pchart->drawScale($scaleSettings); //escalo el grafico y dibujo la escala
	
			//Seleccionamos el tipo de grafico a crear (en este caso un spline)
			//$chart->drawSplineChart();
			$this->pchart->drawLineChart();
		}
		
		//renderizo a un archivo o devuelvo el binario
		public function render($file){
			if ($file) return $this->pchart->render($file);
		}
		
		public function stream(){
			try{
				//renderizo la imagen al browser
				$this->pchart->Stroke();
			}catch(Exception $e){
				die($e);
			}
			
		}
	
	}
?>